package com.example.proj_kefei.Service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.proj_kefei.Entity.User;
import com.example.proj_kefei.Mapper.UserMapper;
import com.example.proj_kefei.Service.UserService;
import com.example.proj_kefei.Util.RedisUtil;
import com.example.proj_kefei.Util.Result;
import com.example.proj_kefei.Util.TokenUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public Result login(User user) {
        user.setUserPassword(SecureUtil.md5(user.getUserPassword()));

        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_account",user.getUserAccount());
        queryWrapper.eq("user_password",user.getUserPassword());

        User user1 = userMapper.selectOne(queryWrapper);

        if (user1 != null) {
            TokenUtil tokenUtil = new TokenUtil();
            String token = tokenUtil.createToken(user1);

            Gson gson = new Gson();
            String userjson = gson.toJson(user1); //将从前端接收到的user1转换成json格式
            redisUtil.setWithExpire(token,userjson,100000); //再将token和json格式的user1存放在redis里面，并设置过期时间

            System.out.println("登录成功，输出token:"+token);
            return new Result(200,"登录成功，返回一个token",token);
        }
        return new Result(500,"登录失败，返回空",null);


    }

    @Override
    public Result register(User user) {
        user.setUserPassword(SecureUtil.md5(user.getUserPassword()));
        int insert = userMapper.insert(user);
        if (insert != 0) {
            return new Result(200,"注册成功",null);
        }
        return new Result(200,"注册失败",null);
    }

    @Override
    public Result exit(String token) {
        System.out.println("！！！！！！！！！！！这里检查是否输出token"+token);
        redisUtil.delete(token);
        return new Result(200,"退出成功",null);
    }

    @Override
    public Result select() {
        return new Result(200,"返回成功",userMapper.selectList(null));
    }

    @Override
    public Result selectList(User user) {
        System.out.println("创建分页对象");
        Page page = new Page(user.getPage(),user.getSize());
        System.out.println("输出这个分页对象:" + page);

        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if (user.getUserName() != null) {
            queryWrapper.like("user_name",user.getUserName());
        }
        if (user.getUserAccount() != null) {
            queryWrapper.like("user_account",user.getUserAccount());
        }
        if (user.getUserPhone() != null) {
            queryWrapper.eq("user_phone",user.getUserPhone());
        }
        userMapper.selectPage(page, queryWrapper);
        List<User> list = page.getRecords();
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("list",list);
        map.put("total",page.getTotal());
        return new Result(200,"查询成功",map);
    }

    @Override
    public Result add(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",user.getUserAccount());

        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 != null) {
            return new Result(200,"新增失败,用户已存在",0);
        }
        user1.setUserPassword(SecureUtil.md5(user.getUserPassword()));
        int num = userMapper.insert(user);
        return new Result(200,"新增成功",num);
    }

    @Override
    public Result delete(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getUserId());
        int num = userMapper.delete(queryWrapper);
        return new Result(200,"删除成功",num);
    }

    @Override
    public Result update(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getUserId());
        int num = userMapper.update(user, queryWrapper);
        return new Result(200,"修改成功",num);
    }


}
